home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / multiprocessing / process.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  292 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Process',
  6.     'current_process',
  7.     'active_children']
  8. import os
  9. import sys
  10. import signal
  11. import itertools
  12.  
  13. try:
  14.     ORIGINAL_DIR = os.path.abspath(os.getcwd())
  15. except OSError:
  16.     ORIGINAL_DIR = None
  17.  
  18.  
  19. def current_process():
  20.     '''
  21.     Return process object representing the current process
  22.     '''
  23.     return _current_process
  24.  
  25.  
  26. def active_children():
  27.     '''
  28.     Return list of process objects corresponding to live child processes
  29.     '''
  30.     _cleanup()
  31.     return list(_current_process._children)
  32.  
  33.  
  34. def _cleanup():
  35.     for p in list(_current_process._children):
  36.         if p._popen.poll() is not None:
  37.             _current_process._children.discard(p)
  38.             continue
  39.     
  40.  
  41.  
  42. class Process(object):
  43.     '''
  44.     Process objects represent activity that is run in a separate process
  45.  
  46.     The class is analagous to `threading.Thread`
  47.     '''
  48.     _Popen = None
  49.     
  50.     def __init__(self, group = None, target = None, name = None, args = (), kwargs = { }):
  51.         if not group is None:
  52.             raise AssertionError, 'group argument must be None for now'
  53.         count = _current_process._counter.next()
  54.         self._identity = _current_process._identity + (count,)
  55.         self._authkey = _current_process._authkey
  56.         self._daemonic = _current_process._daemonic
  57.         self._tempdir = _current_process._tempdir
  58.         self._parent_pid = os.getpid()
  59.         self._popen = None
  60.         self._target = target
  61.         self._args = tuple(args)
  62.         self._kwargs = dict(kwargs)
  63.         if not name:
  64.             pass
  65.         self._name = type(self).__name__ + '-' + ':'.join((lambda .0: for i in .0:
  66. str(i))(self._identity))
  67.  
  68.     
  69.     def run(self):
  70.         '''
  71.         Method to be run in sub-process; can be overridden in sub-class
  72.         '''
  73.         if self._target:
  74.             self._target(*self._args, **self._kwargs)
  75.         
  76.  
  77.     
  78.     def start(self):
  79.         '''
  80.         Start child process
  81.         '''
  82.         if not self._popen is None:
  83.             raise AssertionError, 'cannot start a process twice'
  84.         if not self._parent_pid == os.getpid():
  85.             raise AssertionError, 'can only start a process object created by current process'
  86.         if not not (_current_process._daemonic):
  87.             raise AssertionError, 'daemonic processes are not allowed to have children'
  88.         _cleanup()
  89.         self._popen = Popen(self)
  90.         _current_process._children.add(self)
  91.  
  92.     
  93.     def terminate(self):
  94.         '''
  95.         Terminate process; sends SIGTERM signal or uses TerminateProcess()
  96.         '''
  97.         self._popen.terminate()
  98.  
  99.     
  100.     def join(self, timeout = None):
  101.         '''
  102.         Wait until child process terminates
  103.         '''
  104.         if not self._parent_pid == os.getpid():
  105.             raise AssertionError, 'can only join a child process'
  106.         if not self._popen is not None:
  107.             raise AssertionError, 'can only join a started process'
  108.         res = self._popen.wait(timeout)
  109.  
  110.     
  111.     def is_alive(self):
  112.         '''
  113.         Return whether process is alive
  114.         '''
  115.         if self is _current_process:
  116.             return True
  117.         if not self._parent_pid == os.getpid():
  118.             raise AssertionError, 'can only test a child process'
  119.         if self._popen is None:
  120.             return False
  121.         self._popen.poll()
  122.         return self._popen.returncode is None
  123.  
  124.     
  125.     def name(self):
  126.         return self._name
  127.  
  128.     name = property(name)
  129.     
  130.     def name(self, name):
  131.         if not isinstance(name, str):
  132.             raise AssertionError, 'name must be a string'
  133.         self._name = name
  134.  
  135.     name = name.setter(name)
  136.     
  137.     def daemon(self):
  138.         '''
  139.         Return whether process is a daemon
  140.         '''
  141.         return self._daemonic
  142.  
  143.     daemon = property(daemon)
  144.     
  145.     def daemon(self, daemonic):
  146.         '''
  147.         Set whether process is a daemon
  148.         '''
  149.         if not self._popen is None:
  150.             raise AssertionError, 'process has already started'
  151.         self._daemonic = daemonic
  152.  
  153.     daemon = daemon.setter(daemon)
  154.     
  155.     def authkey(self):
  156.         return self._authkey
  157.  
  158.     authkey = property(authkey)
  159.     
  160.     def authkey(self, authkey):
  161.         '''
  162.         Set authorization key of process
  163.         '''
  164.         self._authkey = AuthenticationString(authkey)
  165.  
  166.     authkey = authkey.setter(authkey)
  167.     
  168.     def exitcode(self):
  169.         '''
  170.         Return exit code of process or `None` if it has yet to stop
  171.         '''
  172.         if self._popen is None:
  173.             return self._popen
  174.         return self._popen.poll()
  175.  
  176.     exitcode = property(exitcode)
  177.     
  178.     def ident(self):
  179.         '''
  180.         Return indentifier (PID) of process or `None` if it has yet to start
  181.         '''
  182.         return self is _current_process if self is _current_process else self._popen.pid
  183.  
  184.     ident = property(ident)
  185.     pid = ident
  186.     
  187.     def __repr__(self):
  188.         if self is _current_process:
  189.             status = 'started'
  190.         elif self._parent_pid != os.getpid():
  191.             status = 'unknown'
  192.         elif self._popen is None:
  193.             status = 'initial'
  194.         elif self._popen.poll() is not None:
  195.             status = self.exitcode
  196.         else:
  197.             status = 'started'
  198.         if type(status) is int:
  199.             if status == 0:
  200.                 status = 'stopped'
  201.             else:
  202.                 status = 'stopped[%s]' % _exitcode_to_name.get(status, status)
  203.         
  204.         if not self._daemonic or ' daemon':
  205.             pass
  206.         return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, status, '')
  207.  
  208.     
  209.     def _bootstrap(self):
  210.         global _current_process
  211.         util = util
  212.         import 
  213.         
  214.         try:
  215.             self._children = set()
  216.             self._counter = itertools.count(1)
  217.             
  218.             try:
  219.                 sys.stdin.close()
  220.                 sys.stdin = open(os.devnull)
  221.             except (OSError, ValueError):
  222.                 pass
  223.  
  224.             _current_process = self
  225.             util._finalizer_registry.clear()
  226.             util._run_after_forkers()
  227.             util.info('child process calling self.run()')
  228.             
  229.             try:
  230.                 self.run()
  231.                 exitcode = 0
  232.             finally:
  233.                 util._exit_function()
  234.  
  235.         except SystemExit:
  236.             e = None
  237.             if not e.args:
  238.                 exitcode = 1
  239.             elif type(e.args[0]) is int:
  240.                 exitcode = e.args[0]
  241.             else:
  242.                 sys.stderr.write(e.args[0] + '\n')
  243.                 sys.stderr.flush()
  244.                 exitcode = 1
  245.         except:
  246.             e.args
  247.             exitcode = 1
  248.             import traceback as traceback
  249.             sys.stderr.write('Process %s:\n' % self.name)
  250.             sys.stderr.flush()
  251.             traceback.print_exc()
  252.  
  253.         util.info('process exiting with exitcode %d' % exitcode)
  254.         return exitcode
  255.  
  256.  
  257.  
  258. class AuthenticationString(bytes):
  259.     
  260.     def __reduce__(self):
  261.         Popen = Popen
  262.         import forking
  263.         if not Popen.thread_is_spawning():
  264.             raise TypeError('Pickling an AuthenticationString object is disallowed for security reasons')
  265.         Popen.thread_is_spawning()
  266.         return (AuthenticationString, (bytes(self),))
  267.  
  268.  
  269.  
  270. class _MainProcess(Process):
  271.     
  272.     def __init__(self):
  273.         self._identity = ()
  274.         self._daemonic = False
  275.         self._name = 'MainProcess'
  276.         self._parent_pid = None
  277.         self._popen = None
  278.         self._counter = itertools.count(1)
  279.         self._children = set()
  280.         self._authkey = AuthenticationString(os.urandom(32))
  281.         self._tempdir = None
  282.  
  283.  
  284. _current_process = _MainProcess()
  285. del _MainProcess
  286. _exitcode_to_name = { }
  287. for name, signum in signal.__dict__.items():
  288.     if name[:3] == 'SIG' and '_' not in name:
  289.         _exitcode_to_name[-signum] = name
  290.         continue
  291.  
  292.